package main.java.indi.zyj.coderecord;

public class Solve {

    public static void main(String[] args) {
        solve(new char[][]{{'O','O','O'},{'O','O','O'},{'O','O','O'}});
    }

    static boolean[][] visited;



    public static void solve(char[][] board) {
        int x = board.length;;
        int y = board[0].length;
        visited = new boolean[x][y];

        for (int i = 0; i < x; i++) {
            dfs(board, i, 0);
            dfs(board, i, y - 1);
        }
        for (int i = 0; i < y; i++) {
            dfs(board, 0, i);
            dfs(board, x - 1, i);
        }

        // 遍历数组，把没有被标记的 'O' 修改成 'X'
        for (int row = 0; row < x; row++) {
            for (int col = 0; col < y; col++) {
                if (board[row][col] == 'O' && !visited[row][col])
                    board[row][col] = 'X';
            }
        }
    }

    public static void dfs(char[][] grid, int i, int j){
        if (i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] == 'X' || visited[i][j])
            return;
        visited[i][j] = true;
        dfs(grid, i - 1, j);
        dfs(grid, i + 1, j);
        dfs(grid, i, j - 1);
        dfs(grid, i, j + 1);
    }



}
